﻿using Android.App;
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using AndroidX.Core.Content;
using Java.Lang;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WSYmi.ConnectAct;
using WSYmi.MainAct;
using Xamarin.Essentials;

namespace WSYmi.TableAct
{
    public class TableAdapter : BaseAdapter<List<string>>
    {
        private readonly List<List<string>> Items;

        private readonly Activity Context;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context">ListView所在Activity</param>
        /// <param name="items">要在列表中显示的项目</param>
        public TableAdapter(Activity context, List<List<string>> items) : base()
        {
            Context = context;
            Items = items;
        }

        public override List<string> this[int position]
        {
            get { return Items[position]; }
        }

        public override int Count
        {
            get { return Items.Count; }
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = Items[position];
            View view = convertView;
            view ??= Context.LayoutInflater.Inflate(Resource.Layout.Table_item, null);//要适配的显示块

            //项目属性赋予控件.Text

            LinearLayout lt = (LinearLayout)view;

            for (int i = 0; i < lt.ChildCount; i++)
            {
                if (lt.GetChildAt(i) is TextView)
                { // 若是Button记录下
                    TextView tv = (TextView)lt.GetChildAt(i);
                    int tag = Integer.ParseInt(tv.Tag.ToString());
                    tv.Text = item.Count > tag ? item[tag] : "";

                    if (position == 0)//标题加粗
                    {
                        tv.Typeface = Typeface.Create((Typeface)null, TypefaceStyle.Bold);
                        tv.SetTextColor(Color.White);
                    }
                    else
                    {
                        tv.Typeface = Typeface.Create((Typeface)null, TypefaceStyle.Normal);
                        tv.SetTextColor(Color.Black);
                    }
                }
            }

            if (position == 0)//标题深低
            {
                view.SetBackgroundColor(Context.Resources.GetColor(Resource.Color.蓝色字, Context.Theme));
            }
            else if (position % 2 == 0)//偶数为浅深
            {
                view.SetBackgroundColor(Context.Resources.GetColor(Resource.Color.浅灰蓝, Context.Theme));
            }
            else//奇数为灰
            {
                view.SetBackgroundColor(Color.White);
            }

            return view;
        }
    }
}